import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { LoginUsers, Users } from '../mockdata/user';
let _Users = Users;
import * as http from '../api/api';

export default {
    /**
     * mock bootstrap
     */
    bootstrap() {
        let mock = new MockAdapter(axios);
        //let mock = new MockAdapter(http);
        // mock success request
        mock.onGet('/success').reply(200, {
            ee: 'success'
        });

        // mock error request
        mock.onGet('/error').reply(500, {
            msg: 'failure'
        });

       // 验证码返回假数据
      mock.onGet("/proxyApi/captcha").reply(200,{
        data:'/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAoAHgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6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ALKIZwPllgYxlT1DDHGQeckUULle7B36GnJMsbKgG+RuRGrAMVyAWwSOBuBP+OBVa+sY7pk2OkU28MW25LKCNw4IPTGDng7SQQMEorNya1QFeXTTbus0VxKqKrBt0pCqOuTggcAYzgke3NQXW/yJlMo8xVOHMgkVD2LKcEjPbI+owKKKaetglojPmuoYpgdMkvLq4cbVt1QsgIPDHOEUZI+bGeR2HGh9h1TU1V7qc6cGULIltjzWAzjMnOMHkY7Eg9aKKp6BbZmjYaZZaZEYrK2SFT1I5LdepPJ6nrVuiipGZmreH9M1rab23DOowsikqwHpkf1qTStFsNGhaKxgEYY5Zsks31Jooou9gL9IyhlKsAQRgg96KKAMseGtEE3nDS7Xf1/1Yx+XStQAKAAAAOABRRRcBaKKKAP/2Q=='
      })

        //登录
        mock.onPost('/proxyApi/login').reply(config => {
          debugger;
            let { username, password,captcha} = JSON.parse(config.data).params;
            return new Promise((resolve, reject) => {
                let user = null;
                let captchaIsCorrect = null;
                setTimeout(() => {
                    let hasUser = LoginUsers.some(u => {
                        if (u.username === username && u.password === password) {
                            if(u.captcha === captcha){
                              captchaIsCorrect = true;
                              user = JSON.parse(JSON.stringify(u));
                              user.password = undefined;
                              user.captcha = undefined;
                              return true;
                            }else{
                              captchaIsCorrect = false
                            }
                        }
                    });
                    debugger;
                    if(captchaIsCorrect != null && !captchaIsCorrect){
                      resolve([200, { code: 500, msg: '验证码不正确', user }]);
                    }
                    if (hasUser) {
                        resolve([200, { code: 200, msg: '请求成功', user }]);
                    } else {
                        resolve([200, { code: 500, msg: '账号或密码错误' }]);
                    }
                }, 500);
            });
        });



        //获取用户列表
        mock.onGet('/user/list').reply(config => {
            let { name } = config.params;
            let mockUsers = _Users.filter(user => {
                if (name && user.name.indexOf(name) == -1) return false;
                return true;
            });
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve([200, {
                        users: mockUsers
                    }]);
                }, 500);
            });
        });

        //获取用户列表（分页）
        mock.onGet('/user/listpage').reply(config => {
            let { page, name } = config.params;
            let mockUsers = _Users.filter(user => {
                if (name && user.name.indexOf(name) == -1) return false;
                return true;
            });
            let total = mockUsers.length;
            mockUsers = mockUsers.filter((u, index) => index < 10 * page && index >= 10 * (page - 1));
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve([200, {
                        total: total,
                        users: mockUsers
                    }]);
                }, 500);
            });
        });

        //删除用户
        mock.onGet('/user/remove').reply(config => {
            let { id } = config.params;
            _Users = _Users.filter(u => u.id !== id);
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve([200, {
                        code: 200,
                        msg: '删除成功'
                    }]);
                }, 500);
            });
        });

        //编辑用户
        mock.onGet('/user/edit').reply(config => {
            let { id, name, addr, age, birth, sex } = config.params;
            _Users.some(u => {
                if (u.id === id) {
                    u.name = name;
                    u.addr = addr;
                    u.age = age;
                    u.birth = birth;
                    u.sex = sex;
                    return true;
                }
            });
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve([200, {
                        code: 200,
                        msg: '编辑成功'
                    }]);
                }, 500);
            });
        });

        //新增用户
        mock.onGet('/user/add').reply(config => {
            let { name, addr, age, birth, sex } = config.params;
            _Users.push({
                name: name,
                addr: addr,
                age: age,
                birth: birth,
                sex: sex
            });
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve([200, {
                        code: 200,
                        msg: '新增成功'
                    }]);
                }, 500);
            });
        });

    }
};
